package com.lc.luzhengtang.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.ForgetBean;
import com.lc.luzhengtang.bean.VerificationCodeBean;
import com.lc.luzhengtang.conn.ConnChangePwd;
import com.lc.luzhengtang.conn.ConnVerificationCode2;
import com.lc.luzhengtang.util.LovelyToast;
import com.lc.luzhengtang.util.Validator;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMatches;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/4.
 */
public class ChangePasswordActivity extends BaseActivity {

    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.text_title)
    TextView textTitle;
    @BindView(R.id.img_phone)
    ImageView imgPhone;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.img_sendcode)
    ImageView imgSendcode;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.layout_send_ben)
    LinearLayout layoutSendBen;
    @BindView(R.id.text_sendbtn)
    TextView textSendbtn;
    @BindView(R.id.img_pwd)
    ImageView imgPwd;
    @BindView(R.id.edit_newpwd)
    EditText editNewpwd;
    @BindView(R.id.img_submit)
    ImageView imgSubmit;
    @BindView(R.id.edit_repwd)
    EditText editRepwd;
    @BindView(R.id.btn_register)
    Button btnRegister;
    boolean is_matcher;
    private CountDownTimer countTime = new CountDownTimer(61000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

            if (!isFinishing()) {

                textSendbtn.setText(millisUntilFinished / 1000 + "s重新获取");

            }
        }

        @Override
        public void onFinish() {

            if (!isFinishing()) {
                textSendbtn.setText("重新获取");
                textSendbtn.setEnabled(true);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actitivity_change_password);
        ButterKnife.bind(this);
        textTitle.setText("修改密码");
    }

    String myCode;

    public void getChangePwd() {
        new ConnChangePwd(editPhone.getText().toString().trim(), editNewpwd.getText().toString().trim(), new AsyCallBack<ForgetBean>() {
            @Override
            public void onSuccess(String toast, int type, Object o, ForgetBean forgetBean) throws Exception {
                super.onSuccess(toast, type, o, forgetBean);
                LovelyToast.makeText(ChangePasswordActivity.this, forgetBean.getMessage(), 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
                finish();
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            /*    LovelyToast.makeText(AppealActivity.this, ConnChangePwd.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);*/
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
             /*   LovelyToast.makeText(AppealActivity.this, ConnChangePwd.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);*/
            }
        }

        ).execute(ChangePasswordActivity.this, false);
    }

    @OnClick({R.id.img_news, R.id.text_title, R.id.img_phone, R.id.edit_phone, R.id.img_sendcode, R.id.edit_code, R.id.layout_send_ben, R.id.text_sendbtn, R.id.img_pwd, R.id.edit_newpwd, R.id.img_submit, R.id.edit_repwd, R.id.btn_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;
            case R.id.text_title:
                break;
            case R.id.img_phone:
                break;
            case R.id.edit_phone:
                break;
            case R.id.img_sendcode:
                break;
            case R.id.edit_code:
                break;
            case R.id.layout_send_ben:
                break;
            case R.id.text_sendbtn:

                if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                    LovelyToast.makeText(ChangePasswordActivity.this, "用户手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(editPhone.getText().toString())) {
                    LovelyToast.makeText(ChangePasswordActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    // UtilToast.show(RegisterActivity.this, "请输入正确的手机号");
                } else {
//                    textSendbtn.setEnabled(false);
                    getVerificationCode();
                }

                break;
            case R.id.img_pwd:
                break;
            case R.id.edit_newpwd:
                break;
            case R.id.img_submit:
                break;
            case R.id.edit_repwd:
                break;
            case R.id.btn_register:
                is_matcher = editNewpwd.getText().toString() != null && editNewpwd.getText().toString().matches("[A-Za-z0-9]+");
                if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                    LovelyToast.makeText(ChangePasswordActivity.this, "用户手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(editPhone.getText().toString())) {
                    LovelyToast.makeText(ChangePasswordActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    // UtilToast.show(RegisterActivity.this, "请输入正确的手机号");
                } else if (TextUtils.isEmpty(editCode.getText().toString().trim())) {
                    LovelyToast.makeText(ChangePasswordActivity.this, "验证码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!editCode.getText().toString().equals(myCode)) {
                    LovelyToast.makeText(ChangePasswordActivity.this, "验证码不正确", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (TextUtils.isEmpty(editNewpwd.getText().toString())) {
                    LovelyToast.makeText(ChangePasswordActivity.this, "密码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!Validator.isPassword(editNewpwd.getText().toString())) {
                    LovelyToast.makeText(ChangePasswordActivity.this, "请输入6~12位数字或英文密码", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (TextUtils.isEmpty(editRepwd.getText().toString())) {
                    LovelyToast.makeText(ChangePasswordActivity.this, "密码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!Validator.isPassword(editRepwd.getText().toString())) {
                    LovelyToast.makeText(ChangePasswordActivity.this, "请输入6~12位数字或英文密码", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!editNewpwd.getText().toString().equals(editRepwd.getText().toString())) {
                    LovelyToast.makeText(ChangePasswordActivity.this, "两次输入的密码不一致", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } /*else if (is_matcher) {
                    LovelyToast.makeText(ChangePasswordActivity.this, "密码应为数字与字母的组合", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } */ else {
                    getChangePwd();

                }
                break;
        }
    }

    public void getVerificationCode() {
        new ConnVerificationCode2(editPhone.getText().toString().trim(), "xiu", BaseApplication.basePreferences.readUid(), new AsyCallBack<VerificationCodeBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
            }

            @Override
            public void onSuccess(String toast, int type, Object o, VerificationCodeBean registerBean) throws Exception {
                super.onSuccess(toast, type, o, registerBean);
              /*  LovelyToast.makeText(ChangePasswordActivity.this, registerBean.getMessage(), 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);

                countTime.start();
                myCode = registerBean.getData().getCode() + "";*/
                if (registerBean.getCode() == 200) {
                    countTime.start();
//                finish();myCode=registerBean.getData().getCode();
                    LovelyToast.makeText(ChangePasswordActivity.this, registerBean.getMessage(), 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    myCode = registerBean.getData().getCode() + "";
                } else {
                    LovelyToast.makeText(ChangePasswordActivity.this, registerBean.getMessage(), 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }


            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                dismissLoading();
                textSendbtn.setEnabled(true);
                textSendbtn.setText("获取验证码");

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                dismissLoading();

            }
        }


        ).execute(this);
    }
}
